home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / s0ftpj / knstat_freebsd.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-17  |  10.4 KB  |  285 lines

  1. /*
  2.  * Name: kerninetstat 
  3.  * Date: Sun Feb 13 13:16:33 2000
  4.  * Author: pIGpEN [pigpen@s0ftpj.org, deadhead@sikurezza.org]
  5.  *
  6.  * SoftProject Digital Security for Y2K (www.s0ftpj.org)
  7.  * Sikurezza.org Italian Security MailingList (www.sikurezza.org)
  8.  * 
  9.  * COFFEE-WARE LICENSE - This source code is like "THE BEER-WARE LICENSE" by
  10.  * Poul-Henning Kamp <phk@FreeBSD.ORG> but you can give me in return a coffee.
  11.  * 
  12.  * Tested on: FreeBSD 4.0-19990705-CURRENT FreeBSD 4.0-19990705-CURRENT #6 i386
  13.  *
  14.  * This simple source code uses sysctlbyname() to fetch statistics of a protocol
  15.  * you can use them for security purposes or for kernel testing... see also
  16.  * sources of systat or netstat -s...
  17.  *
  18.  * Note: some variables of stat structures can be not present in other kernel
  19.  * versions
  20.  */
  21.  
  22. /* 
  23.  * knstat is intended to be used as cron job example:
  24.  *
  25.  * knstat -icmp >> icmp_stat.log
  26.  *
  27.  * if you wanna use this tool like a command define WAIT
  28.  *
  29.  * #define WAIT
  30.  *
  31.  */
  32.  
  33.  
  34.  
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <err.h>
  38. #include <sysexits.h>
  39.  
  40. #include <sys/types.h>
  41. #include <sys/sysctl.h>
  42.  
  43. #include <netinet/in.h>
  44. #include <netinet/in_systm.h>
  45. #include <netinet/ip.h>
  46. #include <netinet/ip_var.h>
  47. #include <netinet/udp.h>
  48. #include <netinet/udp_var.h>
  49. #include <netinet/tcp.h>
  50. #include <netinet/tcp_timer.h>
  51. #include <netinet/tcp_var.h>
  52. #include <netinet/ip_icmp.h>
  53. #include <netinet/icmp_var.h>
  54. #include <netinet/igmp.h>
  55. #include <netinet/igmp_var.h>
  56.  
  57. #define        Error(s)    err(EX_UNAVAILABLE, s);
  58. #define        E(s)        if(!strcmp(s, arg[1]))
  59.  
  60. void    usage        __P((char *));
  61. void    ip_stat        __P((void));
  62. void    udp_stat    __P((void));
  63. void    tcp_stat    __P((void));
  64. void    icmp_stat    __P((void));
  65. void    igmp_stat    __P((void));
  66.  
  67. struct prot {
  68.     char *name;
  69.     void (*funct) (void);
  70. };
  71.     
  72.  
  73. struct prot protos[] = {
  74.     { "-ip"  , ip_stat   },
  75.     { "-udp" , udp_stat  },
  76.     { "-tcp" , tcp_stat  },
  77.     { "-icmp", icmp_stat },
  78.     { "-igmp", igmp_stat },
  79. };
  80.  
  81.     
  82. int main(int narg, char **arg)
  83. {
  84.     int i;
  85.     int len = sizeof(protos) / sizeof(struct prot);
  86.  
  87.     
  88.     if(narg != 2){
  89.         usage(arg[0]);
  90.         exit(0);
  91.     }
  92.  
  93.     /* Think different */
  94.     
  95.     for(i=0; i < len; i++) 
  96.         E(protos[i].name) {    
  97.             (*protos[i].funct) ();    
  98.             return 1;    
  99.         }
  100.  
  101.     usage(arg[0]);
  102.  
  103.     return 0; 
  104. }
  105.  
  106. void usage(char *cmdname)
  107. {
  108.     printf("Usage:\n"
  109.            "\t%s -option\n\n"
  110.            "Option:   -ip\n" 
  111.            "          -icmp\n"
  112.            "          -igmp\n"
  113.            "          -tcp\n"
  114.            "          -udp\n", cmdname);
  115. }           
  116.  
  117. /*
  118.  * You can cover printf() with a macro... but I'm fucking about... so
  119.  * I have time to spend... 
  120.  */
  121.  
  122. void ip_stat(void)
  123. {
  124.  struct ipstat i_stat;
  125.  int len = sizeof(i_stat);
  126.  
  127.  if(sysctlbyname("net.inet.ip.stats", &i_stat, &len, 0, 0) < 0 )
  128.      Error("[ip_stat] sysctlbyname");
  129.  
  130.  printf("IP Statistics\n\n");
  131.  
  132.  printf("\t\treceived         [ %ld ]\n", i_stat.ips_total);
  133.  printf("\t\tbad checksum     [ %ld ]\n", i_stat.ips_badsum);
  134.  printf("\t\tpkts too short   [ %ld ]\n", i_stat.ips_tooshort);
  135.  printf("\t\tno enough data   [ %ld ]\n", i_stat.ips_toosmall);
  136.  printf("\t\tiph len < data   [ %ld ]\n", i_stat.ips_badhlen);
  137.  printf("\t\tip len < iph len [ %ld ]\n", i_stat.ips_badlen);
  138.  printf("\t\tfragments        [ %ld ]\n", i_stat.ips_fragments);
  139.  printf("\t\tfrags dropped    [ %ld ]\n", i_stat.ips_fragdropped);
  140.  printf("\t\tfrags timeout    [ %ld ]\n", i_stat.ips_fragtimeout);
  141.  printf("\t\tforwarded        [ %ld ]\n", i_stat.ips_forward);
  142.  printf("\t\tfast forward     [ %ld ]\n", i_stat.ips_fastforward);
  143.  printf("\t\tcant forward     [ %ld ]\n", i_stat.ips_cantforward);
  144.  printf("\t\tredirect sent    [ %ld ]\n", i_stat.ips_redirectsent);
  145.  printf("\t\tproto unknown    [ %ld ]\n", i_stat.ips_noproto);
  146.  printf("\t\tiplen > maxpksz  [ %ld ]\n", i_stat.ips_toolong);
  147.  printf("\t\tip version != 4  [ %ld ]\n", i_stat.ips_badvers);
  148.  printf("\t\ttotal raw gen    [ %ld ]\n", i_stat.ips_rawout);
  149.  printf("\t\tmcast not memb   [ %ld ]\n", i_stat.ips_notmember);
  150. }
  151.  
  152. void udp_stat(void)
  153. {
  154.  struct udpstat u_stat;
  155.  int len = sizeof(u_stat);
  156.  
  157.  if(sysctlbyname("net.inet.udp.stats", &u_stat, &len, 0, 0) < 0) 
  158.     Error("[udp_stat] sysctlbyname");
  159.  
  160.  printf("UDP Statistics\n\n");
  161.  
  162.  printf("Total input  packets:      %ld\n",        u_stat.udps_ipackets);
  163.  printf("\t\t\t\tPacket shorter than header: %ld\n", u_stat.udps_hdrops);
  164.  printf("\t\t\t\tChecksum error:             %ld\n", u_stat.udps_badsum);
  165.  printf("\t\t\t\tData len larger than pkt:   %ld\n", u_stat.udps_badlen);
  166.  printf("\t\t\t\tNo socket on port:          %ld\n", u_stat.udps_noport);
  167.  printf("\t\t\t\tArrived as broadcast:       %ld\n", u_stat.udps_noportbcast);
  168.  printf("\t\t\t\tNot delivered:              %ld\n", u_stat.udps_fullsock);
  169.  printf("\t\t\t\tMissing pcb cache:          %ld\n", u_stat.udpps_pcbcachemiss);
  170.  printf("\t\t\t\tNot for hashed pcb:         %ld\n\n",u_stat.udpps_pcbhashmiss);
  171.  printf("Total ouput packets:       %ld\n",       u_stat.udps_opackets);
  172.  printf("\t\t\t\tFast path:                  %ld\n", u_stat.udps_fastout);
  173. }    
  174.  
  175. void tcp_stat(void)
  176. {
  177.  struct tcpstat t_stat;
  178.  int len = sizeof(t_stat);
  179.  
  180.  if(sysctlbyname("net.inet.tcp.stats", &t_stat, &len, 0, 0) < 0)
  181.      Error("[tcp_stat] sysctlbyname");
  182.  
  183.  printf("TCP Statistics\n\n");
  184.  
  185.  printf("Connection:\n");
  186.  printf("\t\tinitiated:               [ %ld ]\n", t_stat.tcps_connattempt);
  187.  printf("\t\taccepted:                [ %ld ]\n", t_stat.tcps_accepts);
  188.  printf("\t\testabilished             [ %ld ]\n", t_stat.tcps_connects);
  189.  printf("\t\tdropped                  [ %ld ]\n", t_stat.tcps_drops);
  190.  printf("\t\tembryonic dropped        [ %ld ]\n", t_stat.tcps_conndrops);
  191.  printf("\t\tkeepalive dropped        [ %ld ]\n", t_stat.tcps_keepdrops);
  192.  printf("\t\tclosed                   [ %ld ]\n", t_stat.tcps_closed);
  193.  printf("\nTimers:\n");
  194.  printf("\t\tsegs timed               [ %ld ]\n", t_stat.tcps_segstimed);
  195.  printf("\t\trtt updated              [ %ld ]\n", t_stat.tcps_rttupdated);
  196.  printf("\t\tdelayed acks sent        [ %ld ]\n", t_stat.tcps_delack);
  197.  printf("\t\tdropped in rxmt timeouts [ %ld ]\n", t_stat.tcps_timeoutdrop);
  198.  printf("\t\tretrasmit timeouts       [ %ld ]\n", t_stat.tcps_rexmttimeo);
  199.  printf("\t\tpersist timeouts         [ %ld ]\n", t_stat.tcps_persisttimeo);
  200.  printf("\t\tkeepalive timeouts       [ %ld ]\n", t_stat.tcps_keeptimeo);
  201.  printf("\t\tkeepalive probes sent    [ %ld ]\n", t_stat.tcps_keepprobe);
  202. #ifdef    WAIT 
  203.  getchar();
  204. #endif 
  205.  printf("\nPackets\n");
  206.  printf("\t\tsent                     [ %ld ]\n", t_stat.tcps_sndtotal);
  207.  printf("\t\tdata pkt                 [ %ld ]\n", t_stat.tcps_sndpack);
  208.  printf("\t\tdata bytes               [ %ld ]\n", t_stat.tcps_sndbyte);
  209.  printf("\t\tdata pkt retrasmitted    [ %ld ]\n", t_stat.tcps_sndrexmitpack);
  210.  printf("\t\tdata bytes retrasmitted  [ %ld ]\n", t_stat.tcps_sndrexmitbyte);
  211.  printf("\t\tack only pkts            [ %ld ]\n", t_stat.tcps_sndacks);
  212.  printf("\t\turg only pkts            [ %ld ]\n", t_stat.tcps_sndurg);
  213.  printf("\t\twin update only pkt      [ %ld ]\n", t_stat.tcps_sndwinup);
  214.  printf("\t\tsyn|fin|rst pkt          [ %ld ]\n", t_stat.tcps_sndctrl);
  215.  printf("\t\twindow probes            [ %ld ]\n", t_stat.tcps_sndprobe);
  216.  printf("\n\n");
  217. #ifdef    WAIT 
  218.  getchar();
  219. #endif 
  220.  printf("\t\treceived                 [ %ld ]\n", t_stat.tcps_rcvtotal);
  221.  printf("\t\tpkt in sequence          [ %ld ]\n", t_stat.tcps_rcvpack);
  222.  printf("\t\tbyte in sequence         [ %ld ]\n", t_stat.tcps_rcvbyte);
  223.  printf("\t\tpkt with checksum errors [ %ld ]\n", t_stat.tcps_rcvbadsum);
  224.  printf("\t\tpkt with bad offset      [ %ld ]\n", t_stat.tcps_rcvbadoff);
  225.  printf("\t\tpkt received too short   [ %ld ]\n", t_stat.tcps_rcvshort);
  226.  printf("\t\tduplicate only pkts      [ %ld ]\n", t_stat.tcps_rcvduppack);
  227.  printf("\t\tduplicate only bytes     [ %ld ]\n", t_stat.tcps_rcvdupbyte);
  228.  printf("\t\tpartial duplicate data   [ %ld ]\n", t_stat.tcps_rcvpartduppack);
  229.  printf("\t\tpartial duplicate bytes  [ %ld ]\n", t_stat.tcps_rcvpartdupbyte);
  230.  printf("\t\tout of order pkts        [ %ld ]\n", t_stat.tcps_rcvoopack);
  231.  printf("\t\tout of order bytes       [ %ld ]\n", t_stat.tcps_rcvoobyte);
  232.  printf("\t\tpkts with data after win [ %ld ]\n", t_stat.tcps_rcvpackafterwin);
  233.  printf("\t\tbytes received after win [ %ld ]\n", t_stat.tcps_rcvbyteafterwin);
  234.  printf("\t\tpkts rcvd after close    [ %ld ]\n", t_stat.tcps_rcvafterclose);
  235.  printf("\t\tpkts rcvd win probe      [ %ld ]\n", t_stat.tcps_rcvwinprobe);
  236.  printf("\t\tduplicate acks           [ %ld ]\n", t_stat.tcps_rcvdupack);
  237.  printf("\t\tacks for unsent data     [ %ld ]\n", t_stat.tcps_rcvacktoomuch);
  238.  printf("\t\tacks packets             [ %ld ]\n", t_stat.tcps_rcvackpack);
  239.  printf("\t\tbytes acked by rcvd acks [ %ld ]\n", t_stat.tcps_rcvwinupd);
  240.  printf("\t\tseg dropped due to PAWS  [ %ld ]\n", t_stat.tcps_pawsdrop);
  241.  printf("\t\tbogus syn                [ %ld ]\n", t_stat.tcps_badsyn);
  242.  printf("\t\tresnd due to MTU discov. [ %ld ]\n", t_stat.tcps_mturesent);
  243.  printf("\t\tlisten queue overflow    [ %ld ]\n", t_stat.tcps_listendrop);
  244.  
  245. void icmp_stat(void)
  246. {
  247.  struct icmpstat i_stat;
  248.  int len = sizeof i_stat;
  249.  
  250.  if(sysctlbyname("net.inet.icmp.stats", &i_stat, &len, 0, 0) < 0)
  251.      Error("[icmp_stat] sysctlbyname");
  252.  
  253.  printf("ICMP Statistics\n\n");
  254.  
  255.  printf("\t\tNumber of calls to icmp_error  [ %ld ]\n", i_stat.icps_error);
  256.  printf("\t\ticmp_code out of range         [ %ld ]\n", i_stat.icps_badcode);
  257.  printf("\t\tpkts < ICMP_MINLEN             [ %ld ]\n", i_stat.icps_tooshort);
  258.  printf("\t\tbad checksum                   [ %ld ]\n", i_stat.icps_checksum);
  259.  printf("\t\tbad length                     [ %ld ]\n", i_stat.icps_badlen);
  260.  printf("\t\tnumber of responses            [ %ld ]\n", i_stat.icps_reflect);
  261.  printf("\t\tm/bcast echo requests dropped  [ %ld ]\n", i_stat.icps_bmcastecho);
  262. }
  263.  
  264. void igmp_stat(void)
  265. {
  266.  struct igmpstat i_stat;
  267.  int len = sizeof i_stat;
  268.  
  269.  if(sysctlbyname("net.inet.igmp.stats", &i_stat, &len, 0, 0) < 0)
  270.      Error("[igmp_stat] sysctlbyname");
  271.  
  272.  printf("IGMP Statistics\n\n");
  273.  
  274.  printf("\t\tmessages received       [ %d ]\n", i_stat.igps_rcv_total);
  275.  printf("\t\trcvd with too few bytes [ %d ]\n", i_stat.igps_rcv_tooshort);
  276.  printf("\t\trcvd with bad checksum  [ %d ]\n", i_stat.igps_rcv_badsum);
  277.  printf("\t\trcvd membership queries [ %d ]\n", i_stat.igps_rcv_queries);
  278.  printf("\t\trcvd invalid queries    [ %d ]\n", i_stat.igps_rcv_badqueries);
  279.  printf("\t\trcvd membership reports [ %d ]\n", i_stat.igps_rcv_reports);
  280.  printf("\t\trcvd invalid reports    [ %d ]\n", i_stat.igps_rcv_badreports);
  281.  printf("\t\trcvd rep. for our grps  [ %d ]\n", i_stat.igps_rcv_ourreports);
  282.  printf("\t\tsent membership reports [ %d ]\n", i_stat.igps_snd_reports);
  283. }
  284.